package com.loveprogrammer.eternity.model.mission;

import com.alibaba.fastjson.JSON;
import com.loveprogrammer.eternity.factory.AffixFactory;
import com.loveprogrammer.eternity.factory.EggFactory;
import com.loveprogrammer.eternity.factory.EquipmentFactory;
import com.loveprogrammer.eternity.model.Hero;
import com.loveprogrammer.eternity.model.product.Equipment;
import com.loveprogrammer.eternity.model.product.Goods;
import com.loveprogrammer.eternity.model.product.eggs.Egg;
import com.loveprogrammer.eternity.model.product.equipment.Weapon;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @version 1.0.0
 * @description:
 * @author: eric
 * @date: 2023-01-05 19:03
 **/
public class MissionReward implements IMissionReward {

    private Map<GoodsReword,Integer> rewards;

    private RewardProperties properties;
    private int goldCoin;

    private int diamonds;

    private int exp;

    public MissionReward(Map<GoodsReword, Integer> rewards,RewardProperties properties, int goldCoin,int diamonds,int exp) {
        this.rewards = rewards;
        this.properties = properties;
        this.goldCoin = goldCoin;
        this.diamonds = diamonds;
        this.exp = exp;
    }

    @Override
    public Map<Goods, Integer> getRewards(Hero hero) {
        Map<Goods, Integer> result = generate(rewards,hero);
        return result;
    }

    private Map<Goods, Integer> generate(Map<GoodsReword, Integer> rewards, Hero hero) {
        Map<Goods, Integer> result = new HashMap<>();
        Iterator<Map.Entry<GoodsReword, Integer>> iterator = rewards.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<GoodsReword, Integer> entry = iterator.next();
            GoodsReword reword = JSON.parseObject(JSON.toJSONString(entry.getKey()),GoodsReword.class);
            Integer size = entry.getValue();
            // GenerationType 1 固定 2 随机
            // if(reword.getGenerationType() == 2){
                String goodsTemplateId = reword.getGoodsTemplateId();
                if("egg".equals(reword.getGoodsType())){
                    if(reword.getGenerationType() == 2) {
                        Egg egg = EggFactory.getEgg(Integer.parseInt(goodsTemplateId), null);
                        result.put(egg, size);
                    }
                }
                if("weapon".equals(reword.getGoodsType())){
                    // 随机
                    if(reword.getGenerationType() == 1) {
                        Weapon weapon = EquipmentFactory.getWeaponByHero(hero,reword.getMinLevel(),reword.getMaxLevel(),reword.getGoodsTemplateId());
                        if(weapon != null) {
                            // 添加词缀
                            AffixFactory.setAffix(weapon);
                            result.put(weapon, size);
                        }
                    }
                }
                if("equipment".equals(reword.getGoodsType())){
                    // 随机
                    if(reword.getGenerationType() == 1) {
                        Equipment equipment = EquipmentFactory.getEquipmentByHero(hero,reword.getMinLevel(),reword.getMaxLevel(),reword.getGoodsTemplateId());
                        if(equipment != null) {
                            // 添加词缀
                            AffixFactory.setAffix(equipment);
                            result.put(equipment, size);
                        }
                    }
                }
            }
        //}
        return result;
    }

    @Override
    public int getGoldCoin() {
        return goldCoin;
    }
    @Override
    public int getDiamonds() {
        return diamonds;
    }

    public static void main(String[] args) {
        Map<GoodsReword,Integer> rewards = new HashMap<>();
        GoodsReword goodsReword = new GoodsReword("egg",2,"101",0,0,null);
        rewards.put(goodsReword,1);
        System.out.println(JSON.toJSONString(rewards));
    }

    public int getExp() {
        return exp;
    }

    public void setExp(int exp) {
        this.exp = exp;
    }

    @Override
    public RewardProperties getProperties() {
        return properties;
    }

    public void setProperties(RewardProperties properties) {
        this.properties = properties;
    }
}
